//
//  MSMainViewController.m
//  MyShare
//
//  Created by koki on 15/9/14.
//  Copyright (c) 2015年 MyShare. All rights reserved.
//

#import "MSMainViewController.h"
#import "MSTabBarView.h"
#import "MSHomeViewController.h"
#import "MSTradeViewController.h"
#import "MSMineViewController.h"
#import "MSLeadViewController.h"

@interface MSMainViewController ()
@property MSBaseViewController* curVctr; //当前显示页面
@property MSTabBarView* tabBarView;      //底部TabBar

//**模块**//
@property MSHomeViewController*        homeVctr;
@property MSTradeViewController*       tradeVctr;
@property MSMineViewController*        mineVctr;

@end

@implementation MSMainViewController
@synthesize tabBarView,curVctr;

-(void)initUI
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    //添加底部tabBar
    CGRect rect=CGRectMake(0,SCREENFRAME.size.height-KTabBarHeight, SCREENFRAME.size.width, KTabBarHeight);
    tabBarView=[[MSTabBarView alloc]initWithFrame:rect];
    tabBarView.delegate=(id)self;
    NSString * tabBarPath = [[NSBundle mainBundle] pathForResource:@"tabBar" ofType:@"plist"];
    tabBarView.dataArray = [[NSMutableArray alloc]initWithContentsOfFile:tabBarPath];
    [self.view addSubview:tabBarView];
    tabBarView.backgroundColor=[UIColor whiteColor];
    tabBarView.selectedIndex = self.selectedTabBarIndex;
    [self switchSelected:MSTabBarBtnType_Home];
}

- (void)layoutControllerSubViews {
    if ([UIApplication sharedApplication].statusBarFrame.size.height == 40) {
        tabBarView.top = SCREEN_HEIGHT-KTabBarHeight-20;
    }else {
        tabBarView.top = SCREEN_HEIGHT-KTabBarHeight;
    }
}
/*
- (void)testDataFunc
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];

    //区域
    NSString *chinaAreaFile = [documentsPath stringByAppendingPathComponent:@"chinaAreaFile.plist"];
    if ([fileManager fileExistsAtPath:chinaAreaFile]) {
        netEngine.chinaAreaArray = [[NSMutableArray alloc] initWithContentsOfFile:chinaAreaFile];
    }
    if (netEngine.chinaAreaArray.count<1) {
        [netEngine requestChinaAreaWithTarget:self.VctrID hudView:nil];
    }
    
    //省市
    NSString *provincesFile = [documentsPath stringByAppendingPathComponent:@"provincesFile.plist"];
    if ([fileManager fileExistsAtPath:provincesFile]) {
        netEngine.provincesArray = [[NSMutableArray alloc] initWithContentsOfFile:provincesFile];
    }
    if (netEngine.provincesArray.count<1) {
        [netEngine requestProvinceAndCityWithTarget:self.VctrID hudView:nil];
    }

    //阶段
    NSString *projectStateFile = [documentsPath stringByAppendingPathComponent:@"projectStateFile.plist"];
    if ([fileManager fileExistsAtPath:projectStateFile]) {
        netEngine.projectStateArray = [[NSMutableArray alloc] initWithContentsOfFile:projectStateFile];
    }
    if (netEngine.projectStateArray.count<1) {
        [netEngine requestProjectStateWithTarget:self.VctrID hudView:nil];
    }
    
    //投资
    NSString *projectInvestmentFile = [documentsPath stringByAppendingPathComponent:@"projectInvestmentFile.plist"];
    if ([fileManager fileExistsAtPath:projectInvestmentFile]) {
        netEngine.projectInvestmentArray = [[NSMutableArray alloc] initWithContentsOfFile:projectInvestmentFile];
    }
    if (netEngine.projectInvestmentArray.count<1) {
        [netEngine requestProjectInvestmentWithTarget:self.VctrID hudView:nil];
    }
    
    //时间
    NSString *projectTimeFile = [documentsPath stringByAppendingPathComponent:@"projectTimeFile.plist"];
    if ([fileManager fileExistsAtPath:projectTimeFile]) {
        netEngine.projectTimeArray = [[NSMutableArray alloc] initWithContentsOfFile:projectTimeFile];
    }
    if (netEngine.projectTimeArray.count<1) {
        [netEngine requestProjectTimeWithTarget:self.VctrID hudView:nil];
    }
    
    //行业
    NSString *projectIndustryFile = [documentsPath stringByAppendingPathComponent:@"projectIndustryFile.plist"];
    if ([fileManager fileExistsAtPath:projectIndustryFile]) {
        netEngine.projectIndustryArray = [[NSMutableArray alloc] initWithContentsOfFile:projectIndustryFile];
    }
    if (netEngine.projectIndustryArray.count<1) {
        [netEngine requestProjectIndustryWithTarget:self.VctrID hudView:nil];
    }
}
*/
- (void)updateViewController:(NSNotification *)note
{
    NSDictionary *responseDict = note.userInfo;
    if (responseDict[Receiver] == self.VctrID) {
        if (responseDict[@"ChinaArea"] != nil){
            
        }
        if (responseDict[@"ProjectState"] != nil){
            
        }
        if (responseDict[@"ProjectInvestment"] != nil){
            
        }
        if (responseDict[@"ProjectTime"] != nil){
            
        }
        if (responseDict[@"ProjectIndustry"] != nil){
            
        }
    }
}

//tabBar点击事件
-(void)tabBarView:(MSTabBarView*)tabBar didSelectItemIndex:(NSInteger)itemIndex
{
    MSTabBarBtnType swithItem = MSTabBarBtnType_Home;
    switch (itemIndex) {
        case 0://首页
        {
            swithItem = MSTabBarBtnType_Home;
        }
            break;
        case 1://交易
        {
            swithItem = MSTabBarBtnType_Trade;
        }
            break;
        case 2://我的
        {
            swithItem = MSTabBarBtnType_Mine;
        }
            break;
        default:
            break;
    }
    //handle View
    [self switchSelected:swithItem];
}

-(void)switchSelected:(MSTabBarBtnType)item
{
    if (curVctr)
    {
        [curVctr.view removeFromSuperview];
    }
    
    switch (item)
    {
        case MSTabBarBtnType_Home:
        {
            if (self.homeVctr==nil)
            {
                self.homeVctr=[[MSHomeViewController alloc] init];
            }
            curVctr=self.homeVctr;
        }
            break;
        case MSTabBarBtnType_Trade:
        {
            if (self.tradeVctr==nil)
            {
                self.tradeVctr=[[MSTradeViewController alloc] init];
            }
            curVctr=self.tradeVctr;
        }
            break;
        case MSTabBarBtnType_Mine:
        {
            if (self.mineVctr==nil)
            {
                self.mineVctr=[[MSMineViewController alloc] init];
            }
            curVctr=self.mineVctr;
        }
            break;
            
        default:
            break;
    }
    [self.view insertSubview:curVctr.view belowSubview:tabBarView];
}

- (void)setSelectedTabBarIndex:(int)selectedTabBarIndex
{
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    NSString *loginState = [ud objectForKey:LoginState];
    if (!loginState.length) {
        MSLeadViewController *leadVC = [[MSLeadViewController alloc] init];
        [rootNavigation pushViewController:leadVC animated:NO];
    }
    _selectedTabBarIndex = selectedTabBarIndex;
    tabBarView.selectedIndex = selectedTabBarIndex;
}

#pragma Viewcontroller appear & disappear

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [curVctr viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [curVctr viewWillDisappear:animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [curVctr viewDidAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [curVctr viewDidDisappear:animated];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
